(display "\n========================================\n")

(define (repeated f n)
   (if (= n 1)
       f
       (lambda (x)
           (f ((repeated f (- n 1)) x)))))

(display ((repeated square 4) 2))
(newline)

(define (repeated f n)
    (if (= n 1)
        f
        (lambda (x)
            (let ((g (repeated f (- n 1))))
                (f (g x))))))

(display ((repeated square 4) 2))
(newline)


(define (compose f g)
    (lambda (x) (f (g x))))

(define (repeated f n)
    (if (= n 1)
        f
        (compose f (repeated f (- n 1)))))

(display ((repeated square 4) 2))
(newline)

; 以上都是递归版，下面是迭代版
(define (repeated f n)
    (define (iter i fi)
        (if (= i n)
            fi
            (iter (+ i 1)
                (lambda (x)
                    (f (fi x))))))
    (iter 1 f))

(display ((repeated square 4) 2))
(newline)

(define (repeated f n)
    (define (iter i fi)
        (if (= i n)
            fi
            (iter (+ i 1)
                (compose f fi))))
    (iter 1 f))

(display ((repeated square 4) 2))

(display "\n========================================\n")
